using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Firestarter.DB.Schema.Logic;
using NUnit.Framework;
using Rhino.Mocks;

namespace Firestarter.DB.Schema.Test.Logic
{
    [TestFixture]
    public class TestDBSchemaExtractor_SQL
    {

        //For SQL Server need to change to CONSTRAINT [FK_Table_1_Table_2] FOREIGN KEY([fdafdsf])

        [Test]
        public void Test_CreateSqlForRelationships_TableNameNonNull_SchemaNonNull()
        {
            //---------------Set up test pack-------------------
            const string tableName = "some table";
            const string schemaName = "some schema";
            DBSchemaExtractor_Stub dbSchemaExtractor = new DBSchemaExtractor_Stub();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string relationshipsSQL = dbSchemaExtractor.BuildForeignKeyColumnsSql(null, schemaName, tableName, true);
            //---------------Test Result -----------------------
            StringAssert.Contains("TAB_CONST.TABLE_NAME = '" + tableName + "'", relationshipsSQL);
            StringAssert.Contains("TAB_CONST.TABLE_SCHEMA = '" + schemaName + "'", relationshipsSQL);
        }
        [Test]
        public void Test_CreateSqlForRelationships_TableNameNull_SchemaNonNull()
        {
            //---------------Set up test pack-------------------
            const string schemaName = "some schema";
            DBSchemaExtractor_Stub dbSchemaExtractor = new DBSchemaExtractor_Stub();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string relationshipsSQL = dbSchemaExtractor.BuildForeignKeyColumnsSql(null, schemaName, null, true);
            //---------------Test Result -----------------------
            Assert.IsFalse(relationshipsSQL.Contains("TAB_CONST.TABLE_NAME = '"), relationshipsSQL + " should not contain 'TAB_CONST.TABLE_NAME = '");
            StringAssert.Contains("TAB_CONST.TABLE_SCHEMA = '" + schemaName + "'", relationshipsSQL);
        }

        [Test]
        public void Test_CreateSqlForRelationships_TableNameNonNull_SchemaNull()
        {
            //---------------Set up test pack-------------------
            const string tableName = "some table";
            DBSchemaExtractor_Stub dbSchemaExtractor = new DBSchemaExtractor_Stub();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            string relationshipsSQL = dbSchemaExtractor.BuildForeignKeyColumnsSql(null, null, tableName, true);
            //---------------Test Result -----------------------
            StringAssert.Contains("TAB_CONST.TABLE_NAME = '" + tableName + "'", relationshipsSQL);
            Assert.IsFalse(relationshipsSQL.Contains("TAB_CONST.TABLE_SCHEMA = '"), relationshipsSQL + " should not contain 'TAB_CONST.TABLE_NAME = '");
        }
        [Test]
        public void Test_CreateSqlForRelationships_TableNameNull_SchemaNull()
        {
            //---------------Set up test pack-------------------
            DBSchemaExtractor_Stub dbSchemaExtractor = new DBSchemaExtractor_Stub();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            string relationshipsSQL = dbSchemaExtractor.BuildForeignKeyColumnsSql(null, null, null, true);
            //---------------Test Result -----------------------
            Assert.IsFalse(relationshipsSQL.Contains("TAB_CONST.TABLE_NAME = '"), relationshipsSQL + " should not contain 'TAB_CONST.TABLE_NAME = '");
            Assert.IsFalse(relationshipsSQL.Contains("TAB_CONST.TABLE_SCHEMA = '"), relationshipsSQL + " should not contain 'TAB_CONST.TABLE_SCHEMA = '");
        }

        internal class DBSchemaExtractor_Stub : DBSchemaExtractor
        {
            internal DBSchemaExtractor_Stub()
                : base(MockRepository.GenerateStub<IDbConnection>())
            { }


            /// <summary>
            /// Gets the current Catalog Name from the database connection.
            /// </summary>
            public override string CatalogName
            {
                get { throw new System.NotImplementedException(); }
            }

            /// <summary>
            /// Gets the current Schema Name from the database connection.
            /// </summary>
            public override string SchemaName
            {
                get { throw new System.NotImplementedException(); }
            }

            protected override DbDataAdapter CreateDataAdapter(string sql)
            {
                throw new NotImplementedException();
            }

            public override DataTable GetDatabasesDataTable(string catalogName, string schemaName)
            {
                throw new System.NotImplementedException();
            }

            public new string BuildForeignKeyColumnsSql(string catalogName, string schemaName, string tableName, bool includeCatalog)
            {
                return base.BuildForeignKeyColumnsSql(catalogName, schemaName, tableName, includeCatalog);
            }
        }
    }
}
